﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HttpClientStudy.Model
{
    /// <summary>
    /// API返回基类 工具类
    /// </summary>
    public static class BaseResultUtil
    {
        /// <summary>
        /// 创建 泛型返回基类
        /// </summary>
        public static BaseResult<TData> Create<TData>(TData data, int code = 0, string message = "")
        {
            return new BaseResult<TData>() { Data = data, Code = code, Message = message };
        }

        /// <summary>
        /// 创建 泛型成功返回基类
        /// </summary>
        public static BaseResult<TData> Success<TData>()
        {
            return Success<TData>(default, 1, "成功");
        }

        /// <summary>
        /// 创建 泛型成功返回基类
        /// </summary>
        public static BaseResult<TData> Success<TData>(TData? data)
        {
            return Success(data, 1, "成功");
        }

        /// <summary>
        /// 创建 泛型成功返回基类
        /// </summary>
        public static BaseResult<TData> Success<TData>(TData? data, string message)
        {
            return Success(data, 1, message);
        }

        /// <summary>
        /// 创建 泛型成功返回基类
        /// </summary>
        public static BaseResult<TData> Success<TData>(TData? data, int code, string message)
        {
            return new BaseResult<TData>() { Data = data, Code = code, Message = message };
        }

        /// <summary>
        /// 创建 泛型错误返回基类
        /// </summary>
        public static BaseResult<TData> Error<TData>()
        {
            return Error<TData>(default, 0, "错误");
        }

        /// <summary>
        /// 创建 泛型错误返回基类
        /// </summary>
        public static BaseResult<TData> Error<TData>(TData? data)
        {
            return Error(data, 0, "错误");
        }

        /// <summary>
        /// 创建 泛型错误返回基类
        /// </summary>
        public static BaseResult<TData> Error<TData>(TData? data, int code = 0, string message = "错误")
        {
            return new BaseResult<TData>() { Data = data, Code = code, Message = message };
        }

        /// <summary>
        /// 创建 泛型失败返回基类
        /// </summary>
        public static BaseResult<TData> Fail<TData>()
        {
            return Fail<TData>(default, 0, "失败");
        }

        /// <summary>
        /// 创建 泛型失败返回基类
        /// </summary>
        public static BaseResult<TData> Fail<TData>(TData? data)
        {
            return Fail(data, 0, "失败");
        }

        /// <summary>
        /// 创建 泛型失败返回基类
        /// </summary>
        public static BaseResult<TData> Fail<TData>(TData? data, int code = 0, string message = "失败")
        {
            return new BaseResult<TData>() { Data = data, Code = 0, Message = "失败" };
        }

        /// <summary>
        /// 创建 泛型异常返回基类
        /// </summary>
        public static BaseResult<TData> Exception<TData>(Exception ex)
        {
            return Exception<TData>(ex, default, 0);
        }

        /// <summary>
        /// 创建 泛型异常返回基类
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <param name="ex"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static BaseResult<TData> Exception<TData>(Exception ex, TData? data)
        {
            return Exception(ex, data, 0);
        }

        /// <summary>
        /// 创建 泛型异常返回基类
        /// </summary>
        public static BaseResult<TData> Exception<TData>(Exception ex, TData? data, int code = 0)
        {
            return new BaseResult<TData>() { Data = data, Code = code, Message = $"异常：{ex.Message}" };
        }

        /// <summary>
        /// 创建 泛型默认返回基类
        /// </summary>
        public static BaseResult<TData> Default<TData>(TData? data = default)
        {
            return new BaseResult<TData>() { Code = 0, Message = "", Data = data };
        }
    }
}
